package com.example.task.ui;

import static com.example.task.util.WeChatShareUtils.weChatShareUtils;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.task.bean.Lecture;
import com.example.task.bean.SocketUser;
import com.example.task.bean.User;
import com.example.task.R;
import com.example.task.delegate.SimpleDelegateAdapter;
import com.example.task.handler.AddNumHandler;
import com.example.task.http.Threads.AllActThreads;
import com.example.task.http.Threads.JudgeUserIsSignByActivityIDThread;
import com.example.task.http.Threads.SignUpBackNumByActIdThread;
import com.example.task.http.Threads.SignUpBackNumThread;
import com.example.task.http.Threads.SignUpThread;
import com.example.task.http.Threads.UnSignUpThread;
import com.example.task.socket.SocketActivity;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;

import java.util.Objects;

import okhttp3.FormBody;

public class SignUpActivity extends AppCompatActivity {

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        User user = (User) bundle.getSerializable("User");
        Lecture lecture = (Lecture) bundle.getSerializable("Lecture");
        String id = Integer.toString(user.getIduser());
        System.out.println(id);
//        assert user != null;

        Button button = findViewById(R.id.Button8);//报名

        TextView textView1 = findViewById(R.id.TextView1);
        textView1.setText(lecture.getActivityName());
        TextView textView2 = findViewById(R.id.TextView2);
        textView2.setText(lecture.getSignUpTime() + " ~ " + lecture.getDeadlineForSign());
        TextView textView3 = findViewById(R.id.Time);
        textView3.setText("时间：" + lecture.getInitDate() + " " + lecture.getAtivityTime() + "-" + lecture.getDeadlineForAtivity());
        TextView textView4 = findViewById(R.id.Place);
        textView4.setText("地点：" + lecture.getAtivityPlace());
        TextView textView5 = findViewById(R.id.Principal);
        textView5.setText("负责人工号：" + lecture.getInChargeID());
        TextView textView6 = findViewById(R.id.Phone);
        textView6.setText("联系方式：" + lecture.getInChargeNB());
        TextView textView7 = findViewById(R.id.Type);
        textView7.setText( lecture.getType());
        TextView textView8 = findViewById(R.id.Them);
        textView8.setText(lecture.getTheme());
        TextView textView9 = findViewById(R.id.summery);
        textView9.setText(lecture.getSummery());

        TextView talk = findViewById(R.id.talk);
        talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(SignUpActivity.this, SocketActivity.class);
                SocketUser socketUser = new SocketUser();
                socketUser.setActivityId(lecture.getActivityId());
                socketUser.setRole("student");
                socketUser.setRoleName(user.getName());
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("SocketUser",socketUser);
                intent1.putExtras(bundle1);
                startActivity(intent1);
            }
        });
        Button share = findViewById(R.id.Button9);//share
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SignUpActivity.this,ShareActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        TextView textView10 = findViewById(R.id.sum);
        AddNumHandler handler = new AddNumHandler(textView10, button);
        //更新报名数
        FormBody.Builder builder1 = new FormBody.Builder();
        builder1.add("actID", lecture.getActivityId());
        SignUpBackNumByActIdThread thread1 = new SignUpBackNumByActIdThread(builder1);
        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread1.value);
        JsonObject obj1 = new Gson().fromJson(thread1.value, JsonObject.class);
        textView10.setText("报名人数：" + obj1.get("Num").toString().replace("\"",""));
        //判断报名
        FormBody.Builder builder2 = new FormBody.Builder();
        builder2.add("iduser",Integer.toString(user.getIduser()));
        builder2.add("actID",lecture.getActivityId());
        JudgeUserIsSignByActivityIDThread thread2 = new JudgeUserIsSignByActivityIDThread(builder2);
        thread2.start();
        try {
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread2.value);
        if(Objects.equals(thread2.value, "success")){
            button.setText("取消");
            talk.setVisibility(View.VISIBLE);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(button.getText().toString().equals("报名")){
                    FormBody.Builder builder = new FormBody.Builder();
                    builder.add("username", user.getUsername());
                    builder.add("activityID",lecture.getActivityId());
                    SignUpBackNumThread thread = new SignUpBackNumThread(builder);
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(thread.value);
                    JsonObject obj2 = new Gson().fromJson(thread.value, JsonObject.class);
                    new Thread(() -> {
                        Message message=handler.obtainMessage();
                        message.what=1;
                        message.obj="报名人数：" + obj2.get("HasSignUp").toString().replace("\"","") ;
                        handler.sendMessage(message);//发送信息
                    }).start();
                    Toast.makeText(SignUpActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                    talk.setVisibility(View.VISIBLE);
                }
                else{
                    FormBody.Builder builder = new FormBody.Builder();
                    builder.add("activityID", lecture.getActivityId());
                    builder.add("username", user.getUsername());
                    UnSignUpThread thread = new UnSignUpThread(builder);
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(thread.value);
                    JsonObject obj2 = new Gson().fromJson(thread.value, JsonObject.class);
                    new Thread(() -> {
                        Message message=handler.obtainMessage();
                        message.what=2;
                        message.obj="报名人数：" + obj2.get("HasSignUp").toString().replace("\"","") ;
                        handler.sendMessage(message);//发送信息
                    }).start();
                    talk.setVisibility(View.GONE);
                    Toast.makeText(SignUpActivity.this, "取消报名", Toast.LENGTH_LONG).show();

                }
                //                SignUpThread thread = new SignUpThread(builder);
//                thread.start();
//                try {
//                    thread.join();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(thread.value);
//                Toast.makeText(SignUpActivity.this, "报名成功", Toast.LENGTH_LONG).show();
//                editText.setText("");
            }
        });

    }


}